﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using mshtml;

namespace NoLimit_Brwoser
{
    public partial class FrmMain : Form
    {
        string url = "";
        public FrmMain()
        {
            InitializeComponent();
        }

        private void FrmMail_Resize(object sender, EventArgs e)
        {
            MainTabControl.Width = this.Width - 30;
            //MainTabControl.Height = this.Height - 90;
            MainTabControl.Height = this.Height - 120;
            progressBar1.Location = new Point(MainTabControl.Location.X + MainTabControl.Width - progressBar1.Width
                , MainTabControl.Location.Y + MainTabControl.Height + 1);
        }

        private void btn_enter_Click(object sender, EventArgs e)
        {
            if (rdoCurrent.Checked == true)
            {
                url = txtURL.Text;
                TabPage tp = new TabPage();
                MainTabControl.TabPages.Add(tp);
                MainTabControl.SelectedTab = tp;
                WebBrowser web = new WebBrowser();
                web.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBrowser1_DocumentCompleted);
                web.Navigated += new System.Windows.Forms.WebBrowserNavigatedEventHandler(this.webBrowser1_Navigated);
                web.Navigating += new System.Windows.Forms.WebBrowserNavigatingEventHandler(this.webBrowser1_Navigating);
                web.NewWindow += new System.ComponentModel.CancelEventHandler(this.webBrowser1_NewWindow);
                web.ProgressChanged += new System.Windows.Forms.WebBrowserProgressChangedEventHandler(this.webBrowser1_ProgressChanged);

                web.Dock = DockStyle.Fill;
                web.Parent = tp;
                web.Navigate(url);
            }
            else
            {
                FrmNewWin w = new FrmNewWin(txtURL.Text);
                w.Show();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ((WebBrowser)MainTabControl.SelectedTab.Controls[0]).GoBack();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            ((WebBrowser)MainTabControl.SelectedTab.Controls[0]).GoForward();
        }
        //解除js限制
        private void AddJS(string js)
        {
            HtmlElement ele = ((WebBrowser)MainTabControl.SelectedTab.Controls[0]).Document.CreateElement("script");
            ele.SetAttribute("type", "text/javascript");
            ele.SetAttribute("text", js);
            ((WebBrowser)MainTabControl.SelectedTab.Controls[0]).Document.Body.AppendChild(ele);
        }

        //解除js限制
        private void AddJS2(HtmlDocument hd,string js)
        {
            HtmlElement ele = hd.CreateElement("script");
            ele.SetAttribute("type", "text/javascript");
            ele.SetAttribute("text", js);
            try
            {

                hd.Body.AppendChild(ele);
            }
            catch (System.Exception ex)
            {

            }
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            AddJS("function document.onselectstart(){return true;}");
            AddJS("function document.oncontextmenu() { return true; }");
            AddJS("function document.onkeydown() {if (event.ctrlKey && event.keyCode == 67) {event.returnValue = true; }}");
            AddJS("function document.oncopy(){return true;}");
            AddJS("function document.onpaste(){return true;}");
            try
            {

                HtmlElement ele = ((WebBrowser)sender).Document.GetElementsByTagName("body")[0];
                ele.SetAttribute("onpaste", "return true");
            }
            catch (System.Exception ex)
            {

            }
            MainTabControl.SelectedTab.Text = ((WebBrowser)sender).DocumentTitle;
            this.Text = ((WebBrowser)sender).DocumentTitle;


           

            //将所有的FORM的提交目标，指向本窗体  

            //foreach (HtmlElement form in ((WebBrowser)sender).Document.Forms)
            //{
            //    form.SetAttribute("target", "_self");
            //}

            foreach (HtmlWindow hw in ((WebBrowser)sender).Document.Window.Frames)
            {
                try
                {

                    AddJS2(hw.Document, "function document.onselectstart(){return true;}");
                    AddJS2(hw.Document, "function document.oncontextmenu() { return true; }");
                    AddJS2(hw.Document, "function document.onkeydown() {if (event.ctrlKey && event.keyCode == 67) {event.returnValue = true; }}");
                    AddJS2(hw.Document, "function document.oncopy(){return true;}");
                    AddJS2(hw.Document, "function document.onpaste(){return true;}");

                    HtmlElement ele = hw.Document.GetElementsByTagName("body")[0];
                    ele.SetAttribute("onpaste", "return true");

                }
                catch (System.Exception ex)
                {

                }
            }
            if (chkSpecail.Checked == true)
            {
                IHTMLDocument2 doc = ((WebBrowser)sender).Document.DomDocument as IHTMLDocument2;
                doc.designMode = "On";
            }
        }


        private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
        {
            if ((e.CurrentProgress > 0) && (e.MaximumProgress > 0))
            {
                progressBar1.Maximum = Convert.ToInt32(e.MaximumProgress);
                progressBar1.Step = Convert.ToInt32(e.CurrentProgress);
                progressBar1.PerformStep();
            }
            else if (((WebBrowser)sender).ReadyState == WebBrowserReadyState.Complete)
            {
                progressBar1.Value = 0;
            }
        }

        private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
        }

        private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            if (rdoNew.Checked == true)
            {
                FrmNewWin w = new FrmNewWin(e.Url.ToString());
                w.Show();
                e.Cancel = true;
            }
            
        }

        private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            txtURL.Text = e.Url.ToString();
        }

        private void MainTabControl_DoubleClick(object sender, EventArgs e)
        {
            MainTabControl.TabPages.Remove(MainTabControl.SelectedTab);
        }

        private void MainTabControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Text = MainTabControl.SelectedTab.Text;
        }

        private void btnInfo_Click(object sender, EventArgs e)
        {
            FrmInfo info = new FrmInfo();
            info.ShowDialog();

        }


    }
}
